package com.beetlestech.nfe.main.biz.com.impl;

import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.CollectionUtil;
import com.beetlestech.nfe.main.biz.com.HbSmsVerify;
import com.beetlestech.nfe.main.biz.com.dto.VsmsverifyInfoDTO;
import com.beetlestech.nfe.main.biz.com.util.JsonSmsClient;
import com.beetlestech.nfe.main.biz.com.util.RandomUtil;
import com.beetlestech.nfe.main.biz.constant.CommonConfigConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.SmsverificationDTO;
import com.beetlestech.nfe.main.db.tbl.support.SmsverificationDTOSupport;
import com.beetlestech.nfe.main.db.view.VsmsverifyDTO;
import com.beetlestech.nfe.main.db.view.support.VsmsverifyDTOSupport;

public class HbSmsVerifyImpl extends AbstractDBLogic implements HbSmsVerify, CtgCodeConstant, HbMessageCode, CommonConfigConstant, SQLConstant {

	@Override
	public void sendTemplateSms(String phone) {
		
		String verifyCode = RandomUtil.getVerifyCode();
		
		// 验证码登录DB
		SmsverificationDTO updateDTO = new SmsverificationDTO();
		updateDTO.setphone(phone);
		updateDTO.setverifyCode(verifyCode);
		updateDTO.setdeleteflg(0);
		updateDTO.setsenddate(getCurrentDate());
		updateDTO.setsendtime(getCurrentTime());		
		updateDAO.updateOne(SmsverificationDTO.INSERT, updateDTO, M10040WS);
		
		// 发送验证码到手机
		sendTemplateSMS(phone, verifyCode);
				
	}
	
	@Override
	public VsmsverifyInfoDTO checkSmsVerifyCode(String phone, String smsCode) {
		
		VsmsverifyInfoDTO resultDto = new VsmsverifyInfoDTO();
		
		VsmsverifyDTO vsmsverifyDto = new VsmsverifyDTO();
		vsmsverifyDto.setphone(phone);
		vsmsverifyDto.setverifyCode(smsCode);
		List<VsmsverifyDTO> vverifyList = queryDAO.queryForObjectList(VsmsverifyDTOSupport.EQUAL_SEARCH, vsmsverifyDto);
		if (CollectionUtil.isEmpty(vverifyList)) {
			SmsverificationDTO smsverificationDto = new SmsverificationDTO();
			smsverificationDto.setphone(phone);
			smsverificationDto.setverifyCode(smsCode);
			smsverificationDto.setdeleteflg(0);
			List<SmsverificationDTO> verificationList = queryDAO.queryForObjectList(SmsverificationDTOSupport.EQUAL_SEARCH, smsverificationDto);
			if (CollectionUtil.isEmpty(verificationList)){
				// 验证码不存在
				resultDto.setChkResult(1);
			} else {
				// 验证码已过期
				for (SmsverificationDTO updDto : verificationList) {
					if (updDto.getdeleteflg() == 0) {
						updDto.setdeleteflg(1);
						updateDAO.updateOne(SmsverificationDTO.UPDATE, updDto, M10040WS);
					}
				}
				resultDto.setChkResult(2);
			}
		} else {
			// 验证成功
			for (VsmsverifyDTO vDto : vverifyList) {
				SmsverificationDTO updDto = new SmsverificationDTO();
				updDto.setid(vDto.getid());
				updDto.setdeleteflg(1);
				updateDAO.updateOne(SmsverificationDTO.UPDATE, updDto, M10040WS);
			}
			resultDto.setChkResult(0);
		}
		return resultDto;
	}
	
	private void sendTemplateSMS(String to, String param){

		try {
			String result = JsonSmsClient.getInstance().templateSMS(to, param);
			
			System.out.println("Response content is: " + result);
			if (!result.contains(SUCCESS_CODE)) {
				createApplicationException("M10101WC", "短信验证码");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			createApplicationException("M10101WC", "短信验证码");
		}
		
	}
	
}
